
//
// permission
// 区区电脑
// 2023 年 02 月 01 日 09:41
import store from "@/store";

/**
 * 用户是否拥有操作权限
 * @param value
 * @returns {boolean}
 */
export function hasPermission(value) {
    // 如果用户是管理员，则不用处理权限
    const userInfo = store.getters.userInfo;
    if (userInfo.isAdmin) return true;
    const buttonPermission = store.getters.buttonPermission;
    const checkPermissionArray = value.split("|");
    for (let i = 0; i < checkPermissionArray.length; i++) {
        const checkPermission = checkPermissionArray[i];
        const hasIndex = buttonPermission.findIndex(item => item === checkPermission);
        if (hasIndex < 0) console.warning("没有操作权限: ", checkPermission)
        if (hasIndex > -1) return true
    }
    return false
}

/**
 * 管理员是否拥有操作权限
 * @param value
 * @returns {boolean}
 */
export function adminHasPermission(value) {
    // 不管是不是管理员，必须处理权限
    const buttonPermission = store.getters.buttonPermission;
    const checkPermissionArray = value.split("|");
    for (let i = 0; i < checkPermissionArray.length; i++) {
        const checkPermission = checkPermissionArray[i];
        const hasIndex = buttonPermission.findIndex(item => item === checkPermission);
        if (hasIndex < 0) console.warning("没有操作权限: ", checkPermission)
        if (hasIndex > -1) return true
    }
    return false
}

/**
 * 用户是否拥有页面权限
 * @param value
 * @returns {boolean}
 */
export function hasPagePermission(value) {
    // 如果用户是管理员，则不用处理权限
    const userInfo = store.getters.userInfo;
    if (userInfo.isAdmin) return true;
    const pagePermission = store.getters.pagePermission;
    const checkPermissionArray = value.split("|");
    for (let i = 0; i < checkPermissionArray.length; i++) {
        const checkPermission = checkPermissionArray[i];
        const hasIndex = pagePermission.findIndex(item => item === checkPermission);
        if (hasIndex < 0) console.warning("没有页面权限: ", checkPermission)
        if (hasIndex > -1) return true
    }
    return false
}

/**
 * 管理员是否拥有页面权限
 * @param value
 * @returns {boolean}
 */
export function adminHasPagePermission(value) {
    // 不管是不是管理员，必须处理权限
    const pagePermission = store.getters.pagePermission;
    const checkPermissionArray = value.split("|");
    for (let i = 0; i < checkPermissionArray.length; i++) {
        const checkPermission = checkPermissionArray[i];
        const hasIndex = pagePermission.findIndex(item => item === checkPermission);
        if (hasIndex < 0) console.warning("没有页面权限: ", checkPermission)
        if (hasIndex > -1) return true
    }
    return false
}
